﻿using System;
using System.Collections.Generic;
using System.Text;
using BCCommon;
using BCData.Common.Area;
using BCData.CRM.CustomerInfo;
using BCDto.CRM.CustomerInfo;
using BCEntity.CRM.CustomerInfo;
using BCService.Utils;
using System.Linq;
using BCDto.Sim.Admin.Admin;
using BCEntity.Sim.Admin.Admin;
using BCData.CRM.CustomerFollowUpRecord;
using BCService.Common.Area;

namespace BCService.CRM.CustomerInfo
{
    public class CustomerInfoService : ICustomerInfoService
    {
        private ICustomerInfoData cCustomerInfoData;
        private IProvinceData cProvinceData;
        private ICityData cCityData;
        private ICustomerFollowUpRecordData cCustomerFollowUpRecordData;
        private IZoneService zoneService;
        public CustomerInfoService(ICustomerInfoData ccCustomerInfoData, IProvinceData ccProvinceData, ICityData ccCityData, ICustomerFollowUpRecordData ccCustomerFollowUpRecordData, IZoneService zoneService)
        {
            cCustomerInfoData = ccCustomerInfoData;
            cProvinceData = ccProvinceData;
            cCityData = ccCityData;
            cCustomerFollowUpRecordData = ccCustomerFollowUpRecordData;
            this.zoneService = zoneService;
        }

        #region AddNew
        /// <summary>
        /// 接待新增添加客户信息
        /// </summary>
        /// <param name="admin"></param>
        /// <param name="customerInfoRequestDto"></param>
        /// <returns></returns>
        public CustomerInfoDto AddNew(AdminDto  admin, CustomerInfoRequestDto  customerInfoRequestDto)
        {
            var entity = customerInfoRequestDto.As<CustomerInfoEntity>();
            CheckInput(entity);

            var fClist = cCustomerInfoData.GetAllAsync(admin.CompanyId).GetAwaiter().GetResult();
            if (fClist.Count(s => s.UserName == entity.UserName && s.CompanyId == admin.CompanyId && s.Phone == entity.Phone) > 0)
                throw new ArgumentException("客户名称电话重复!");

            entity.CreateTime = DateTime.Now;
            entity.UpdateTime = DateTime.Now;
            entity.BelongStartDate = DateTime.Now;
            entity.BelongEndDate = DateTime.Now.AddMonths(3);

            entity.CompanyId = admin.CompanyId;
            entity.AccountStatus = BCCommon.Enums.AccountStatus.Using.ToString();
            
            entity.ReceptionTimes = 1;
            entity.CustomerLevel = BCCommon.CRMEnums.CRMCustomerLevel.A.ToString();
            entity.CustomerGrade = BCCommon.CRMEnums.CRMCustomerGrade.MainCustomer.ToString();
       
            entity.AdminId = admin.AdminId;
            entity.AdminFullName = admin.FullName;
            entity.IsFill = false;
            entity.IsGivenUp = false;
            entity.FillTimes = 0;
            entity.IsExceedTime = false;

            return cCustomerInfoData.InsertAsync(entity).GetAwaiter().GetResult().As<CustomerInfoDto>();
        }
        #endregion

        #region CheckInput 
        private void CheckInput(CustomerInfoEntity entity)
        {
            if (string.IsNullOrWhiteSpace(entity.ProvinceCode))
                throw new ArgumentNullException("省ID不能是空!");
            var fProvince = cProvinceData.GetProvince(entity.ProvinceCode) ?? throw new ArgumentException("查不到 省名称!");
            entity.ProvinceName = fProvince.ProvinceName;


            if (string.IsNullOrWhiteSpace(entity.CityCode))
                throw new ArgumentException("查不到市名称!");
            var f1 = cCityData.GetCity(entity.CityCode) ?? throw new ArgumentException("市ID不能是空!");
            entity.CityName = f1.CityName;

            entity.CountyCode = "";
            entity.CountyName = "";
             
            if (string.IsNullOrWhiteSpace(entity.UserName))
                throw new ArgumentException("客户名称不能是空");
             
            if (string.IsNullOrWhiteSpace(entity.Phone))
                throw new ArgumentException("客户电话不能是空!");

            if (string.IsNullOrWhiteSpace(entity.Address))
                throw new ArgumentException("客户地址不能是空!");
             
        }
        #endregion

        #region Update
        public CustomerInfoDto Update(CustomerInfoDto ccDto,AdminDto admin)
        {
            var fEntity = ccDto.As<CustomerInfoEntity>();
            CheckInput(fEntity);
            fEntity.UpdateTime = DateTime.Now;
            if (fEntity.CompanyId <= 0)
                fEntity.CompanyId = admin.CompanyId;
            var fClist = cCustomerInfoData.GetAllAsync(admin.CompanyId).GetAwaiter().GetResult();
            if (fClist.Count(s => s.UserName == fEntity.UserName && s.CompanyId == fEntity.CompanyId && s.Phone == fEntity.Phone) > 1)
                throw new ArgumentException("客户名称电话重复!");

            return cCustomerInfoData.UpdateAsync(fEntity).GetAwaiter().GetResult().As<CustomerInfoDto>();
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="ccAdmin"></param>
        /// <param name="ccCustomerInfoId"></param>
        /// <returns></returns>
        public void Delete(AdminDto ccAdmin, long ccCustomerInfoId)
        {
            var cFollowUp = cCustomerFollowUpRecordData.GetListByCustomerAsync(ccCustomerInfoId, 1, 10).GetAwaiter().GetResult();
            if (cFollowUp.Item1 > 0) throw new ArgumentException("要删除的客户有跟进记录,不能删除!"); 
            cCustomerInfoData.Delete(ccCustomerInfoId).GetAwaiter().GetResult(); 
        }
        #endregion

        #region GetByID
        public CustomerInfoDto GetByID(long ccCustomerInfoId)
        {
            var a = cCustomerInfoData.GetByIdAsync(ccCustomerInfoId).GetAwaiter().GetResult();
            var fDto = a.As<CustomerInfoDto>(); 
            return fDto;
        } 
    #endregion


        #region Quest
        public PageableList<CustomerInfoDto> GetList(
                    AdminDto ccAdmin,
                    string UserName,
                    string Phone,
                    string ResourceType,
                    string CustomerType,
                    bool? IsFill,
                    string ProvinceCode,
                    string CityCode,
                    string CountyCode,
                    int pageIndex,
                    int pageSize)
        { 

            var (fCount, fList) = cCustomerInfoData.GetListAsync(ccAdmin.As<AdminEntity>(),ccAdmin.CompanyId,UserName,Phone,ResourceType,CustomerType,IsFill, ProvinceCode, CityCode, CountyCode,pageIndex,pageSize).GetAwaiter().GetResult();

            var fDto = fList.As<List<CustomerInfoDto>>();
         
            return new PageableList<CustomerInfoDto>()
            {
                Count = fCount,
                Items = fDto,
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now
            };
        }
        #endregion 
    }
}
